<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <div slot="header" class="clearfix">
          <span>公司设置</span>
        </div>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button type="primary" size="small" @click="showDialog = true"> + 新增角色</el-button>
            </el-row>
            <el-table
              :data="list"
              border
              style="width: 100%"
            >
              <el-table-column
                prop="date"
                label="序号"
                width="180"
                align="center"
                type="index"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                align="center"
                width="180"
              />
              <el-table-column
                prop="description"
                label="描述"
                align="center"
              />
              <el-table-column
                prop="address"
                align="center"
                label="操作"
              >
                <template slot-scope="{ row }">
                  <el-button size="mini" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="mini" type="primary" @click="updateRole(row.id)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                layout="prev, pager, next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changepage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form abel-width="120px" style="margin-top:50px" label-width="80px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 弹窗 -->
          <el-dialog title="编辑部门" :visible="showDialog" @close="btnCancel">
            <el-form ref="roleForm" :model="roleData" :rules="rules" label-width="120px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleData.name" />
              </el-form-item>
              <el-form-item prop="description" label="角色描述">
                <el-input v-model="roleData.description" />
              </el-form-item>
            </el-form>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button size="small" @click="btnCancel">取消</el-button>
                <el-button size="small" type="primary" @click="btnOk">确认</el-button>
              </el-col>
            </el-row>
          </el-dialog>
        </el-tabs>
      </el-card>
      <!-- 角色权限弹出层 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="8">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/premission'
import { tranListToTreeData } from '@/utils/toTreeData'
export default {
  data() {
    return {
      list: [],
      // 页码及其相关设置
      page: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      formData: {}, // 公司信息
      roleData: {
        naem: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      showDialog: false,
      permData: [], // 权限信息数据
      defaultProps: {
        label: 'name'
      }, // 显示字段的名称 和 赋予属性的字段名称
      showPermDialog: false, // 角色分配弹出层
      userId: null, //
      selectCheck: [] // 默认选中的权限点
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 换页
    changepage(page) {
      this.page.page = page
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.$store.getters.companyId)
    },
    // 删除角色
    async  deleteRole(id) {
      console.log(id)
      try {
        await this.$confirm('确认删除该角色吗?')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        this.$message({
          message: '取消',
          center: true
        })
      }
    },
    // 编辑
    async updateRole(id) {
      this.roleData = await getRoleDetail(id)
      this.showDialog = true
    },
    // 关闭弹窗
    btnCancel() {
      this.showDialog = false
      this.roleData = {
        name: '',
        description: ''
      }
    },
    // 提交弹窗数据
    async btnOk() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleData.id) {
          // 判断有无id 有id 为编辑
          await updateRole(this.roleData)
        } else {
          // 无id 是添加
          await addRole(this.roleData)
        }
        this.getRoleList()
        this.showDialog = false
      } catch (error) {
        console.log(error.message)
      }
    },
    // 角色分配弹窗的关闭方法
    btnPermCancel() {
      this.showPermDialog = false
    },
    async btnPermOK() {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.userId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    // 打开角色分配弹窗层
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.userId = id // 暂存id
      // 通过 getRoleDetail 方法 获取 该角色对应的权限点
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    }
  }
}
</script>

<style>

</style>
